/*
给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序
的方式存储的，并且每个节点只能存储 一位 数字。
请你将两个数相加，并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外，这两个数都不会以 0 开头。

## 示例 1：

输入：l1 = [2,4,3], l2 = [5,6,4]
输出：[7,0,8]
解释：342 + 465 = 807.

## 示例 2：

输入：l1 = [0], l2 = [0]
输出：[0]

## 示例 3：

输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出：[8,9,9,9,0,0,0,1]
*/

#pragma once

#include <iostream>

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
   public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int carry = 0;
        auto ret = new ListNode();
        auto next = ret;
        auto prev = ret;
        do {
            auto val = l1->val + l2->val + carry;
            carry = val / 10;
            val = val % 10;
            next->val = val;
            next->next = new ListNode();
            prev = next;
            next = next->next;
            l1 = l1->next;
            l2 = l2->next;
        } while (l1 != nullptr && l2 != nullptr);
        auto remains = (l1 != nullptr) ? l1 : l2;
        while (remains != nullptr) {
            auto val = remains->val + carry;
            carry = val / 10;
            val = val % 10;
            next->val = val;
            next->next = new ListNode();
            prev = next;
            next = next->next;
            remains = remains->next;
        }
        if (carry != 0) {
            next->val = carry;
        } else {
            prev->next = nullptr;
            delete next;
        }
        return ret;
    }

    ListNode* aiSolution(ListNode* l1, ListNode* l2) {
        // AI-generated solution
        int carry = 0;
        ListNode* dummyHead = new ListNode(0);
        ListNode* current = dummyHead;

        while (l1 != nullptr || l2 != nullptr || carry > 0) {
            int sum = carry;
            if (l1 != nullptr) {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2 != nullptr) {
                sum += l2->val;
                l2 = l2->next;
            }
            carry = sum / 10;
            current->next = new ListNode(sum % 10);
            current = current->next;
        }

        ListNode* result = dummyHead->next;
        delete dummyHead;  // Clean up the dummy head
        return result;
    }

    ListNode* officialSolution(ListNode* l1, ListNode* l2) {
        ListNode *head = nullptr, *tail = nullptr;
        int carry = 0;
        while (l1 || l2) {
            int n1 = l1 ? l1->val : 0;
            int n2 = l2 ? l2->val : 0;
            int sum = n1 + n2 + carry;
            if (!head) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail->next = new ListNode(sum % 10);
                tail = tail->next;
            }
            carry = sum / 10;
            if (l1) {
                l1 = l1->next;
            }
            if (l2) {
                l2 = l2->next;
            }
        }
        if (carry > 0) {
            tail->next = new ListNode(carry);
        }
        return head;
    }
};

int answer(ListNode* l1, ListNode* l2) {
    auto s = Solution();
    auto ret = s.addTwoNumbers(l1, l2);
    do {
        std::cout << ret->val << " ";
        ret = ret->next;
    } while (ret != nullptr);
    return 0;
}

int ai_answer(ListNode* l1, ListNode* l2) {
    auto s = Solution();
    auto ret = s.aiSolution(l1, l2);
    do {
        std::cout << ret->val << " ";
        ret = ret->next;
    } while (ret != nullptr);
    return 0;
}

int official_answer(ListNode* l1, ListNode* l2) {
    auto s = Solution();
    auto ret = s.officialSolution(l1, l2);
    do {
        std::cout << ret->val << " ";
        ret = ret->next;
    } while (ret != nullptr);
    return 0;
}